package com.kwum.springcloud.cloud.microserviceprovideruser.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.kwum.springcloud.cloud.microserviceprovideruser.entity.SpringcloudUser;
import com.kwum.springcloud.cloud.microserviceprovideruser.repository.UserRepository;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;

/**
 * Description: 用户控制器
 * @author Kwum
 * @date 2017年6月30日 下午2:17:05
 * @version 1.0
 */

@RestController
@RequestMapping(value = "springcloud", produces="application/json;charset=UTF-8")
public class UserController {

    @Autowired
    private UserRepository userRepository;
    
    @Autowired
    private EurekaClient eurekaClient;
    
    /**
     * 根据用户id显示用户信息
     * @param id 用户id
     * @return 用户对象
     * @author kwum
     */
    @GetMapping("user/{id}")
    public SpringcloudUser selectById(@PathVariable Integer id){
        return this.userRepository.findOne(id);
    }
    
    /**
     * 获取当前服务的ip地址、端口信息、应用名称以及状态
     * @return 当前服务的ip地址、端口信息、应用名称以及状态
     * @author kwum
     */
    @GetMapping("/eureka/instance/info")
    public String getServiceUrlByServiceName(){
        InstanceInfo instanceInfo = eurekaClient.getNextServerFromEureka("microservice-provider-user", false);
        String result = "";
        result += "ipAddress : " + instanceInfo.getIPAddr() + "\n";
        result += "port : " + instanceInfo.getPort() + "\n";
        result += "appName : " + instanceInfo.getAppName() + "\n";
        result += "status : " + instanceInfo.getStatus();
        return result;
    }
}
